// JavaScript Document
function goBack(){
	history.back();
	return false;
}

function showToast(message,isLongToast){
	if(isLongToast){
		window.plugins.ToastPlugin.show_long(message);	
	}else{
		window.plugins.ToastPlugin.show_short(message);			
	}
}


//override default configurations jQuery Mobile
$(document).bind("mobileinit", function(){
	//apply overrides here
	$.mobile.defaultPageTransition = "none";
	$.mobile.buttonMarkup.hoverDelay = 0;
});
//disable scrolling
//$(document).bind("touchmove",function(event){
//	event.preventDefault();
//});


// Cordova is ready
//
document.addEventListener("deviceready",onDeviceReady, false);
var db;
function onDeviceReady() {
	checkConnection();
	document.addEventListener("resume", onResume, false);

	var remembered = window.localStorage.getItem("remember");
	//alert(remembered);
	if(remembered=="true"){
		$.mobile.changePage($("#main_menu"));
	}
	document.addEventListener("backbutton", function(e){
	    if($.mobile.activePage.is('#main_menu') || $.mobile.activePage.is('#login')){
	        e.preventDefault();
	        showExitConfirm();
		}else {
	        navigator.app.backHistory();
	    }
	}, false);    
	//shoppinglist database initialization
	db = window.openDatabase(db_name, db_version, db_display_name, db_size);
    db.transaction(populateDB, errorCB, successCB);
}

function onResume(){
	//check network connection
	checkConnection();	
}
function checkConnection() {
	var networkState = navigator.network.connection.type;
	
	if(networkState == Connection.NONE){
		console.log('connection lost!');
		window.plugins.ToastPlugin.show_long('Internet connection lost...');
		//implement tap here to try again page
	}else{
	}
}


function onExitConfirm(button) {
    if(button==1){//first button(OK) selected
        navigator.app.exitApp();
    }
}

// Show a custom confirmation dialog
//
function showExitConfirm() {
    navigator.notification.confirm(
        'Exit App?',  // message
        onExitConfirm,              // callback to invoke with index of button pressed
        'Confirm',            // title
        'OK,Cancel'          // buttonLabels
    );
}

